package com.leoman.controller.company;

import com.leoman.common.DataTableFactory;
import com.leoman.controller.common.CommonController;
import com.leoman.core.Constant;
import com.leoman.entity.CompanyUser;
import com.leoman.entity.Video;
import com.leoman.entity.vo.RegionVo;
import com.leoman.service.VideoRecordService;
import com.leoman.service.VideoService;
import com.leoman.utils.DateUtils;
import com.leoman.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 视频管理
 * Created by 涂奕恒 on 2016/11/02.
 */
@Controller
@RequestMapping(value = "company/video")
public class CompanyVideoController extends CommonController {

    @Autowired
    private VideoService videoService;

    @Autowired
    private VideoRecordService videoRecordService;

    @RequestMapping("index")
    public String index(HttpServletRequest request, ModelMap model) {
        CompanyUser companyUser = (CompanyUser) request.getSession().getAttribute(Constant.CURRENT_ADMIN_USER);

        if (null == companyUser) {
            return "redirect:/company/login";
        }

        model.addAttribute("companyUserId", companyUser.getId());
        return "company/视频列表";
    }

    @RequestMapping("/list")
    public void list(HttpServletResponse response,
                     Integer companyUserId,
                     String name,
                     Integer videoTypeId,
                     Integer status,
                     Integer draw,
                     Integer start,
                     Integer length) {
        RegionVo regionVo;

        if (null == start || start == 0) {
            start = 1;
        }
        int pageNum = getPageNum(start, length);
        Page<Video> page = videoService.pageByParams(companyUserId, name, null, videoTypeId, null, status, null, pageNum, length);

        for (Video video : page.getContent()) {
            regionVo = videoService.getVideoStatus(video);
            video.setTempStatus(regionVo.getId());
            video.setTempContent(regionVo.getName());
        }

        Map<String, Object> result = DataTableFactory.fitting(draw, page);
        WebUtil.printJson(response, result);
    }

    @RequestMapping("show")
    public String show(ModelMap model, Integer videoId) {
        if (null != videoId) {
            Video video = videoService.getById(videoId);
            model.addAttribute("video", video);

            // 查询该视频的投放记录
            model.addAttribute("videoRecordList", videoRecordService.findListByVideoId(videoId));

            return "company/视频详情";
        } else {
            return "company/添加视频";
        }
    }

    @RequestMapping("record")
    public String record(ModelMap model, Integer videoId) {
        if (null != videoId) {
            Video video = videoService.getById(videoId);
            model.addAttribute("video", video);
        }

        return "company/投放视频";
    }

    @RequestMapping("save")
    @ResponseBody
    public Integer save(HttpServletRequest request, String name, Integer sendType, Integer videoTypeId, MultipartRequest multipartRequest, String synopsis) {
        try {
            CompanyUser companyUser = (CompanyUser) request.getSession().getAttribute(Constant.CURRENT_ADMIN_USER);

            if (null == companyUser) {
                return -1;
            }

            return videoService.saveVideo(null, companyUser.getId(), name, multipartRequest, videoTypeId, sendType, synopsis);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @RequestMapping("saveRecord")
    @ResponseBody
    public Integer saveRecord(Integer videoId, String regionIds, String regionNames, String startTime, Double price, Integer count, Integer isOnly, Integer isSend) {
        try {
            videoService.iSendVideo(videoId, null, regionIds, regionNames, DateUtils.stringToDateWithFormat(startTime, "yyyy-MM-dd hh:mm").getTime(), null, price, count, isOnly, isSend);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 删除视频
     *
     * @param videoId 视频ID
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Integer delete(Integer videoId) {
        try {
            return videoService.deleteByVideoId(videoId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 修改视频上架状态
     *
     * @param videoId 视频ID
     * @return
     */
    @RequestMapping("/changeShopping")
    @ResponseBody
    public Integer changeShopping(Integer videoId, Integer status) {
        try {
            return videoService.changeVideoShopping(videoId, status);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
